<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="rules" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科室id" prop="deptId">
        <el-cascader
          placeholder="请输入执行科室"
          size="small"
          :value="deptIds"
          clearable
          :props="props"
          @change="handleChange"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="医生id" align="center" prop="userId" width="150"/>
        <el-table-column label="医生名称" align="center" prop="nickName" width="150"/>
        <el-table-column label="星期一上午" align="center" prop="first0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1" false-label="0" v-model="scope.row.first0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期一下午" align="center" prop="first1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1" false-label="0" v-model="scope.row.first1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期二上午" align="center" prop="second0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1" v-model="scope.row.second0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期二下午" align="center" prop="second1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1" v-model="scope.row.second1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期三上午" align="center" prop="thirdly0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1" v-model="scope.row.thirdly0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期三下午" align="center" prop="thirdly1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1" v-model="scope.row.thirdly1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期四上午" align="center" prop="fourthly0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.fourthly0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期四下午" align="center" prop="fourthly1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.fourthly1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期五上午" align="center" prop="fifth0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.fifth0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期五下午" align="center" prop="fifth1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.fifth1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期六上午" align="center" prop="sixth0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.sixth0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期六下午" align="center" prop="sixth1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.sixth1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期日上午" align="center" prop="seventh0" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.seventh0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="星期日下午" align="center" prop="seventh1" width="70">
          <template slot-scope="scope">
            <el-checkbox true-label="1"  v-model="scope.row.seventh1"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="排班限额" align="center" >
          <template slot-scope="scope">
            <el-input v-model="scope.row.skLimit"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="listUser"
      />
      <el-form-item prop="ruleName" label="规则名称">
        <el-input v-model="form.ruleName" placeholder="规则名称"/>
      </el-form-item>
      <el-form-item prop="description" label="规则描述">
        <el-input v-model="form.description" placeholder="规则描述"/>
      </el-form-item>
      <el-button type="primary" :disabled="multiple" @click="submitForm">新增排班规则</el-button>
    </el-form>
  </div>
</template>

<script>
import { listSkd_rule_item, getSkd_rule_item, delSkd_rule_item, addSkd_rule_item, updateSkd_rule_item, exportSkd_rule_item } from "@/api/sms/skd_rule_item";
import { listSkd_rule, getSkd_rule, delSkd_rule, addSkd_rule, updateSkd_rule, exportSkd_rule} from "@/api/sms/skd_rule";
import {
  listDept,
  getDept,
  delDept,
  addDept,
  updateDept,
  exportDept,
  listTypeName,
  listTypeNameC,
  listChildren
} from "../../../api/sms/dept";
import { listUser, getUser, delUser, addUser, updateUser, exportUser, resetUserPwd, changeUserStatus, importTemplate } from "@/api/system/user";
export default {
  name: "Skd_rule_item",
  data() {
    let _this = this
    return {
      //输入框
      input: '',
      //多选框
      checked: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排版规则详情表格数据
      skd_rule_itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffId: null,
        daysOfWeek: null,
        status: null,
        skLimit: null,
        skRuleId: null,
        type_name:null,
        operatorId:null,
        ruleName:null,
        description:null,
        deptId:null
      },
      ppss:"555",
      querys:{
        parentId:null,
        deptId:null,
      },
      deptIds: [],
      props: {
        lazy: true,
        value: 'deptId',
        label: 'deptName',
        lazyLoad (node, resolve) {
          const { level, value } = node; // 获取当前node对象中的level和value（部门id）属性
          let nodes = null
          if(node.root) {
            _this.getChildren(0, resolve) // 查询根节点，parentId等于0
          } else {
            _this.getChildren(value, resolve) // 根据parentId查询子节点
          }
        }
      },
      userList:[],
      //表格行数据
      skdruleitemList: [],
      //医生对象数组
      dockers: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // this.getList();
    this.listUser();
  },
  methods: {
    /**下拉框的change事件*/
    handleChange(value){
      this.deptIds = value;
      this.querys.parentId=this.deptIds[1]
      this.querys.deptId=this.deptIds[2]
      listDept(this.querys).then(response=>{
        this.queryParams.deptId=response.data[0].deptId;
      })
    },
    getChildren(parentId, resolve) {
      listChildren(parentId).then(response => {
        resolve(response.data);
      })
    },
    listUser(){
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询排版规则详情列表 */
    getList() {
      this.loading = true;
      listSkd_rule_item(this.queryParams).then(response => {
        this.skd_rule_itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffId: null,
        daysOfWeek: null,
        status: 0,
        skLimit: null,
        skRuleId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.skdruleitemList = selection.map(item=>item)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排版规则详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

    },
    /** 提交按钮 */
    submitForm() {
      if(this.queryParams.deptId == null){
        this.$message("请选择科室！");
        return;
      }

      this.form.deptId = this.queryParams.deptId;
      var sklimitbooean = true;
      for(var i = 0;i<this.ids.length;i++){
        var str ="";
        var temp = this.skdruleitemList[i];
        str+='{"staffId":'+this.ids[i]+',"daysOfWeek":"';
        str+=temp.first0==undefined?0:temp.first0;
        str+=temp.first1==undefined?0:temp.first1;
        str+=temp.second0==undefined?0:temp.second0;
        str+=temp.second1==undefined?0:temp.second1;
        str+=temp.thirdly0==undefined?0:temp.thirdly0;
        str+=temp.thirdly1==undefined?0:temp.thirdly1;
        str+=temp.fourthly0==undefined?0:temp.fourthly0;
        str+=temp.fourthly1==undefined?0:temp.fourthly1;
        str+=temp.fifth0==undefined?0:temp.fifth0;
        str+=temp.fifth1==undefined?0:temp.fifth1;
        str+=temp.sixth0==undefined?0:temp.sixth0;
        str+=temp.sixth1==undefined?0:temp.sixth1;
        str+=temp.seventh0==undefined?0:temp.seventh0;
        str+=temp.seventh1==undefined?0:temp.seventh1;
        if(temp.skLimit==undefined || temp == ""){
          this.$message("请输入对应的排班限额");
          this.dockers = "";
          return;
        }else{
          str+='",';
          str+='"skLimit":'+temp.skLimit+'}';
        }
        console.log(str)
        this.dockers[i]  = JSON.parse(str);
      }

      this.form.smsSkdRuleltemList=[];
      this.form.smsSkdRuleltemList=this.dockers
      console.log(this.form)
      this.$refs["queryForm"].validate(valid => {
        if(valid){
          addSkd_rule(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("排班规则新增成功");
            }else{
              this.msgSuccess("系统异常");
            }
          });
        }
      });
      this.dockers="";
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除排版规则详情编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSkd_rule_item(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有排版规则详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSkd_rule_item(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
